﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToWeb.CApExportToWebUtilities
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Reflection;
using System.Resources;
using Tecnomatix.Engineering;
using Tecnomatix.Engineering.PrivateImplementationDetails;
using Tecnomatix.Engineering.Ui.WPF;
using Tecnomatix.Planning;


namespace DnProcessSimulateCommands.ExportToWeb;

public class CApExportToWebUtilities
{
  private static string _outputRegistryCaption = "ExportToWebFolderPath";
  private static string _LodRegistryValue = "ExportToWebLodValue";
  private static string _launchInBrowserRegistryValue = "ExportToWebLaunchInBrowser";
  private static string _saveToFileRegistryValue = "ExportToWebSaveToFile";
  private static string _saveToTCServerRegistryValue = "ExportToWebSaveToTCServer";

  public static ResourceManager GetResourceManager()
  {
    return new ResourceManager("DnProcessSimulateCommands.ExportToWeb.ExportToWebStringTable", Assembly.GetCallingAssembly());
  }

  public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
  {
    DirectoryInfo directoryInfo1 = new DirectoryInfo(sourceDirName);
    DirectoryInfo[] directories = directoryInfo1.GetDirectories();
    if (!directoryInfo1.Exists)
      throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
    if (!Directory.Exists(destDirName))
      Directory.CreateDirectory(destDirName);
    foreach (FileInfo file in directoryInfo1.GetFiles())
    {
      string destFileName = Path.Combine(destDirName, file.Name);
      file.CopyTo(destFileName, true);
    }
    if (!copySubDirs)
      return;
    foreach (DirectoryInfo directoryInfo2 in directories)
    {
      string destDirName1 = Path.Combine(destDirName, directoryInfo2.Name);
      CApExportToWebUtilities.DirectoryCopy(directoryInfo2.FullName, destDirName1, copySubDirs);
    }
  }

  public static void AttachHTMLFileToProcessInTC(
    string fileToAttach,
    string dataSetName,
    ITxOperation op)
  {
    try
    {
      ITxPlanningObject planingObject = CApExportToWebUtilities.GetPlaningObject(op);
      if (planingObject == null || !File.Exists(fileToAttach))
        return;
      planingObject.AddFileAttachment((TxFileAttachmentData) new TxTcFileAttachmentData(fileToAttach, dataSetName)
      {
        DatasetType = "HTML",
        DatasetRole = "IMAN_reference"
      });
    }
    catch (Exception ex)
    {
    }
  }

  private static ITxPlanningObject GetPlaningObject(ITxOperation op)
  {
    ITxProcessModelObject processModelObject = op as ITxProcessModelObject;
    if (!(op is ITxPlanningObject planingObject) && processModelObject != null)
      planingObject = processModelObject.PlanningRepresentation;
    return planingObject;
  }

  public static string GetSafeFilename(string filename)
  {
    return string.Join("_", filename.Split(Path.GetInvalidFileNameChars()));
  }

  public static bool IsGraphicViewerEmpty()
  {
    TxObjectList txObjectList = new TxObjectList();
    foreach (ITxDisplayableObject allDescendant in (Collection<ITxObject>) TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (ITxDisplayableObject))))
    {
      if (allDescendant.Visibility == null || allDescendant.Visibility == 1)
        ((Collection<ITxObject>) txObjectList).Add((ITxObject) allDescendant);
    }
    return ((Collection<ITxObject>) txObjectList).Count == 0;
  }

  public static void StoreValuesToRegistry(TxWindow window, CApExportToWebViewModel data)
  {
    if (data == null)
      return;
    string str;
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      str = data.OutputFileName;
    }
    else
    {
      try
      {
        str = Directory.GetParent(data.OutputFileName).FullName;
      }
      catch (Exception ex)
      {
        str = CApExportToWebUtilities.GetSafeFilename(data.OutputFileName);
      }
    }
    int selectedLod = data.SelectedLOD;
    bool loadBrowser = data.LoadBrowser;
    TxWindowSettings.StoreWindowValue(window, CApExportToWebUtilities._outputRegistryCaption, (object) str);
    TxWindowSettings.StoreWindowValue(window, CApExportToWebUtilities._LodRegistryValue, (object) selectedLod);
    TxWindowSettings.StoreWindowValue(window, CApExportToWebUtilities._launchInBrowserRegistryValue, (object) loadBrowser);
    if (TxTcUtilities.IsTcAnyPlatform)
      TxWindowSettings.StoreWindowValue(window, CApExportToWebUtilities._saveToFileRegistryValue, (object) data.SaveToFileCheckedState);
    TxWindowSettings.StoreWindowValue(window, CApExportToWebUtilities._saveToTCServerRegistryValue, (object) data.SaveToServerCheckBox);
  }

  public static void LoadValuesFromRegistry(TxWindow window, ref CApExportToWebViewModel data)
  {
    if (data == null)
      return;
    CApExportToWebUtilities.RestoreDialogOutputPaths(window, ref data);
    CApExportToWebUtilities.RestoreDialogLOD(window, ref data);
    data.LoadBrowser = CApExportToWebUtilities.GetBoolValueFromRegistry(window, CApExportToWebUtilities._launchInBrowserRegistryValue, data.LoadBrowser);
    if (TxTcUtilities.IsTcAnyPlatform)
    {
      CApExportToWebViewModel exportToWebViewModel = data;
      TxWindow window1 = window;
      string fileRegistryValue = CApExportToWebUtilities._saveToFileRegistryValue;
      bool? fileCheckedState = data.SaveToFileCheckedState;
      bool flag = true;
      int num = fileCheckedState.GetValueOrDefault() == flag & fileCheckedState.HasValue ? 1 : 0;
      bool? nullable = new bool?(CApExportToWebUtilities.GetBoolValueFromRegistry(window1, fileRegistryValue, num != 0));
      exportToWebViewModel.SaveToFileCheckedState = nullable;
    }
    CApExportToWebViewModel exportToWebViewModel1 = data;
    TxWindow window2 = window;
    string serverRegistryValue = CApExportToWebUtilities._saveToTCServerRegistryValue;
    bool? toServerCheckBox = data.SaveToServerCheckBox;
    bool flag1 = true;
    int num1 = toServerCheckBox.GetValueOrDefault() == flag1 & toServerCheckBox.HasValue ? 1 : 0;
    bool? nullable1 = new bool?(CApExportToWebUtilities.GetBoolValueFromRegistry(window2, serverRegistryValue, num1 != 0));
    exportToWebViewModel1.SaveToServerCheckBox = nullable1;
  }

  private static void RestoreDialogOutputPaths(TxWindow window, ref CApExportToWebViewModel data)
  {
    string path1 = "";
    object obj;
    TxWindowSettings.RestoreWindowValue(window, CApExportToWebUtilities._outputRegistryCaption, ref obj);
    if (obj != null)
    {
      path1 = obj as string;
      if (TxTcUtilities.IsTcAnyPlatform)
        data.OutputNameInTC = TxApplication.ActiveDocument.PhysicalRoot.Name + "_WebView";
      else
        path1 = TxApplication.ActiveDocument.CurrentStudy == null ? Path.Combine(path1, "_WebView.html") : Path.Combine(path1, TxApplication.ActiveDocument.CurrentStudy.Caption + "_WebView.html");
    }
    else if (TxTcUtilities.IsTcAnyPlatform)
      data.OutputNameInTC = TxApplication.ActiveDocument.PhysicalRoot.Name + "_WebView";
    data.OutputFileName = path1;
  }

  private static void RestoreDialogLOD(TxWindow window, ref CApExportToWebViewModel data)
  {
    object obj;
    TxWindowSettings.RestoreWindowValue(window, CApExportToWebUtilities._LodRegistryValue, ref obj);
    if (obj == null)
      return;
    switch ((int) obj)
    {
      case 0:
        data.SelectedLOD = 0;
        break;
      case 1:
        data.SelectedLOD = 1;
        break;
      case 2:
        data.SelectedLOD = 2;
        break;
    }
  }

  private static bool GetBoolValueFromRegistry(
    TxWindow window,
    string TxKeyName,
    bool defaultValue)
  {
    object obj;
    TxWindowSettings.RestoreWindowValue(window, TxKeyName, ref obj);
    return obj != null ? Convert.ToBoolean(obj) : defaultValue;
  }
}
